package cn.vshare.service.impl;

import cn.vshare.dao.CommentDao;
import cn.vshare.entity.Comment;
import cn.vshare.entity.User;
import cn.vshare.entity.Video;
import cn.vshare.exception.AlreadyLikeException;
import cn.vshare.exception.NotLikeException;
import cn.vshare.service.CommentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Auther: 张恒
 * @Date: 2018/9/17 12:09
 * @Description:service接口实现
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;

    //分页查找评论
    @Override
    public PageInfo<Comment> queryAllComment(String videoId, Integer pageNum, Integer pageSize) {
        PageInfo pageInfo = null;
        if (!videoId.equals("")) {
            PageHelper.startPage(pageNum, pageSize);
            List<Comment> list = commentDao.queryAllComment(videoId);
            pageInfo = new PageInfo(list);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Comment> queryAllCommentByParseCount(String videoId, Integer pageNum, Integer pageSize) {
        PageInfo pageInfo = null;
        if (!videoId.equals("")) {
            PageHelper.startPage(pageNum, pageSize, true);
            List<Comment> list = commentDao.queryAllCommentByParseCount(videoId);
            pageInfo = new PageInfo(list);
        }
        return pageInfo;
    }

    //分页查找热门评论

    /**
     * 插入新的comment,当插入成功时,返回新插入的commentId
     *备注:9月30日:修改了判断条件和将date set进comment中
     * @param user
     * @param videoId
     * @param context
     * @param date
     * @return
     */

    @Override
    public Comment addComment(User user, String videoId, String context, String date) {
        if (context != null && !context.equals("")) {
            String commentId = UUID.randomUUID().toString();
            Video video = new Video();
            video.setId(videoId);
            Comment comment = new Comment();
            comment.setId(commentId);
            comment.setContext(context);
            comment.setUser(user);
            comment.setVideo(video);
            comment.setCreateDate(date);
            String newCommentId = commentDao.addComment(comment) ? commentId : "";
            if (!newCommentId.equals("")) {
                Comment newComment = commentDao.queryCommentById(newCommentId);
                return newComment;
            }
        }
        return null;
    }

    @Override
    public boolean updateCommentParseCount(String commentId, Integer parseCount) {
        return commentDao.updateCommentParseCount(commentId, parseCount);
    }


    //取消赞
    @Override
    public boolean updateCommentAuditStatus(String commentId, Integer auditStatus) {
        return commentDao.updateCommentAuditStatus(commentId, auditStatus);
    }

    //当举报时,改变评论内容"该评论已删除",虚假的删除,但是变更内容
    @Override
    public boolean updateCommentByInform(String commentId) {
        return commentDao.updateCommentByInform(commentId);
    }

    //删除评论,真的删除
    @Override
    public boolean delComment(String commentId) {
        return commentDao.delComment(commentId);
    }

    //改变状态,假的删除,不变更内容
    @Override
    public boolean updateCommentStatus(String commentId, Integer status) {
        return status != null && commentDao.updateCommentStatus(commentId, status);
    }

    @Override
    public Page<Comment> getCommentList(Map<String, Object> map) {
        Page<Comment> commentPage = new Page<Comment>();
        int totalRows = commentDao.countComment(map); //获取总行数
        int pageSize = (int) map.get("pageSize"); //获取每页显示条数
        int currPage = (int) map.get("currPage"); //获取当前页面
        int totalPages = totalRows % pageSize == 0 ? totalRows / pageSize : totalRows / pageSize + 1; //计算中页数
        commentPage = PageHelper.startPage(currPage,pageSize);
        List<Comment> commentList = commentDao.getCommentList(map);
        return commentPage;
    }

    @Override
    public Integer queryCommentCount(String videoId) {
        return commentDao.queryCommentCount(videoId);
    }

    @Override
    public int queryCommentIfLike(String commentId, User user) {
        return commentDao.queryCommentIfLike(commentId, user);
    }

    @Override
    public Comment queryCommentById(String commentId) {
        if (commentId != null && !commentId.equals("")) {
            return commentDao.queryCommentById(commentId);
        }
        return null;
    }


    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public boolean updateCommentAddLike(User user, String commentId) throws Exception {
        int ifOrNot = commentDao.queryCommentIfLike(commentId, user);
        if (ifOrNot == 0) {
            String id = UUID.randomUUID().toString();
            Comment comment = commentDao.queryCommentById(commentId);
            if (comment == null) {
                throw new Exception();
            } else {
                Integer parseCount = comment.getParseCount();
                if (commentDao.updateCommentAddLike(id, user, commentId)) {
                    parseCount += 1;
                    return commentDao.updateCommentParseCount(commentId, parseCount);
                }
            }
        } else {
            throw new AlreadyLikeException("已经点过赞!");
        }
        return false;
    }


    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public boolean updateCommentCancelLike(User user, String commentId) throws Exception {
        int ifOrNot = commentDao.queryCommentIfLike(commentId, user);
        if (ifOrNot == 1) {
            Comment comment = commentDao.queryCommentById(commentId);
            if (comment == null) {
                throw new Exception();
            }
            Integer parseCount = comment.getParseCount();
            if (commentDao.updateCommentCancelLike(user, commentId)) {
                if (parseCount >= 1) {
                    parseCount -= 1;
                    return commentDao.updateCommentParseCount(commentId, parseCount);
                }
            }
        } else {
            throw new NotLikeException("没有点赞!");
        }
        return false;

    }

    @Override
    public void delCommentById(String id) {
        commentDao.delCommentById(id);
    }
}
